package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.entity.Dept;
import com.jiyun.entity.User;
import com.jiyun.service.IDeptService;
import com.jiyun.service.IUserService;
import com.jiyun.vo.DeptVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-05-05
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    IDeptService deptService;

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addDept(@RequestBody Dept dept){
        dept.setState("启用");
        dept.setCreateTime(new Date());
        deptService.save(dept);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateDept(@RequestBody Dept dept){
        deptService.updateById(dept);
    }


    @Autowired
    IUserService userService;
    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public String deleteOne(@RequestParam Integer id){


        /**
         * 1、是否有子部门
         * 2、是否有用户数据使用当前部门
         */

        LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deptLambdaQueryWrapper.eq(Dept::getPid,id);
        int count = deptService.count(deptLambdaQueryWrapper);
        if (count>0) {
            return "当前要删除的部门有子部门，不能删除";
        }

        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getDid,id);
        count = userService.count(userLambdaQueryWrapper);
        if (count>0) {
            return "当前要删除的部门下有用户，不能删除";
        }

        deptService.removeById(id);
        return "OK";
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        deptService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public Dept findById(@RequestParam Integer id){
        return deptService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<DeptVo> findAll(@RequestBody Dept dept){


        if (StringUtils.isNotBlank(dept.getName())||StringUtils.isNotBlank(dept.getState())) {

            LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = new LambdaQueryWrapper<>();
            deptLambdaQueryWrapper.like(StringUtils.isNotBlank(dept.getName()),Dept::getName,dept.getName());
            deptLambdaQueryWrapper.eq(StringUtils.isNotBlank(dept.getState()),Dept::getState,dept.getState());

            List<Dept> list = deptService.list(deptLambdaQueryWrapper);
            ArrayList<DeptVo> deptVos = new ArrayList<>();
            for (Dept dept1 : list) {
                DeptVo deptVo = new DeptVo();
                //从dept中复制相同的属性值到deptVo中
                BeanUtils.copyProperties(dept1,deptVo);
                deptVos.add(deptVo);
            }
            return deptVos;
        }




        List<DeptVo> deptVoList = findByPid(deptService.list(), -1);
        return deptVoList;
    }


    /**
     * 就是从deptList取找出这个pid的所有子级
     * @param deptList
     * @param pid
     * @return
     */
    public List<DeptVo> findByPid(List<Dept> deptList,Integer pid){
        ArrayList<DeptVo> deptVos = new ArrayList<>();
        for (Dept dept : deptList) {
            if (dept.getPid().equals(pid)) {
                DeptVo deptVo = new DeptVo();
                //从dept中复制相同的属性值到deptVo中
                BeanUtils.copyProperties(dept,deptVo);
                List<DeptVo> son = findByPid(deptList, dept.getId());
                if(CollectionUtils.isNotEmpty(son)){
                    deptVo.setChildren(son);
                }
                deptVos.add(deptVo);
            }
        }
        return deptVos;


    }


    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<Dept> findPage(@RequestParam Integer page,@RequestParam Integer pageSize){
        Page<Dept> deptPage = new Page<>(page, pageSize);
        return deptService.page(deptPage);
    }

}
